//
// Created by fangp on 3/6/18.
//


#include "runner_ds.h"
#include "../../src/ds/graph.h"

static void test_alloc_graph_node() {
    GraphNode *node = alloc_graph_node();
    assert_non_null(node);
}

static void test_init_graph() {
    Graph *g = init_graph();
    assert_non_null(g);
}

static void test_add_vertex() {
    Graph *g = init_graph();
    add_vertex(g, "a", "data_of_a");
    //
    add_vertex(g, "b", "data_of_b");
    //
}

static void test_add_edge() {
    Graph *g = init_graph();
    add_vertex(g, "a", "data_of_a");
    add_vertex(g, "b", "data_of_b");
    add_vertex(g, "c", "data_of_c");
    add_edge(g, "a", "b");
    add_edge(g, "c", "b");
    //
}

/**
 * A->B->C->D->E
 * F
 * A->D
 * C->E
 */
static void test_topological_sort() {
    Graph *g = init_graph();
    add_vertex(g, "A", "data_of_a");
    add_vertex(g, "B", "data_of_b");
    add_vertex(g, "C", "data_of_c");
    add_vertex(g, "D", "data_of_d");
    add_vertex(g, "E", "data_of_e");
    add_vertex(g, "F", "data_of_f");

    add_edge(g, "A", "D");
    add_edge(g, "D", "B");
    add_edge(g, "E", "B");
    add_edge(g, "E", "C");
    add_edge(g, "F", "C");
    add_edge(g, "F", "A");

    Queue* queue = topological_sort(g);
    QueueNode* queueNode = queue->head;
    while (queueNode != NULL) {
        GraphNode* graphNode = (GraphNode *)queueNode->element;
        printf("%s\n", graphNode->id);
        queueNode = queueNode->next;
    }
}

int main(int argc, char **argv) {
    const struct CMUnitTest tests[] = {
            cmocka_unit_test(test_topological_sort),
    };
    return cmocka_run_group_tests(tests, NULL, NULL);
}
